package com.java.test.util;

import com.alibaba.fastjson.JSONArray;

/**
 * IpUtil
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/7/24 0024 11:53
 */
public class IpUtil {

    /*public static void main(String[] args) {
        JSONArray s = IpUtil.fun1("127.0.0.1","127.0.0.9");
        System.out.println(s.toString());
    }*/

    /**
     * ip转long
     * @author
     * @date 2020/7/24 0024 14:04
     * @return long
     */
    public static long getIp2long(String ip) {
        ip = ip.trim();//去空格
        String[] ips = ip.split("\\.");
        long ip2long = 0L;
        for (int i = 0; i < 4; ++i) {
            ip2long = ip2long << 8 | Integer.parseInt(ips[i]);
        }
        return ip2long;
    }

    /**
     * ip转long
     * @param ip
     * @author
     * @date 2020/7/24 0024 14:05
     * @return long
     */
    public static long getIp2long2(String ip) {
        ip = ip.trim();
        String[] ips = ip.split("\\.");
        long ip1 = Integer.parseInt(ips[0]);
        long ip2 = Integer.parseInt(ips[1]);
        long ip3 = Integer.parseInt(ips[2]);
        long ip4 = Integer.parseInt(ips[3]);
        long ip2long =1L* ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;
        return ip2long;
    }

    /**
     * 根据ip段获取ip
     * @param ipStart
     * @param ipEnd
     * @author lijie1
     * @date 2020/7/24 0024 14:06
     * @return java.lang.String
     */
    public static JSONArray fun1(String ipStart, String ipEnd){
        /*PrintStream ps = null;
        try {
            ps = new PrintStream(new File("C:\\IP.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
        JSONArray ipArray = new JSONArray();
        int ipstart=ip2int(ipStart);
        int ipend=ip2int(ipEnd);
        System.out.println(ipend-ipstart+1);
        String []result=new String[ipend-ipstart+1];
        for(int i=0;i<result.length;i++){
            String ip = int2ip(ipstart+i);
            String[] ipArr = ip.split("\\.");
            if("0".equals(ipArr[3]) || "255".equals(ipArr[3])){
                continue;
            }
            ipArray.add(ip);
            //ps.println(ip);
        }
        return ipArray;
    }

    /**
     * int转ip
     * @param l
     * @author lijie1
     * @date 2020/7/24 0024 14:15
     * @return java.lang.String
     */
    public static String int2ip(int l){
        //System.out.println("l--> " +l);
        int a = l/(256*256*256);
        int b = (l-a*256*256*256)/(256*256);
        int c = (l-a*256*256*256-b*256*256)/256;
        int d = (l-a*256*256*256-b*256*256-c*256);
        //return l/(256*256*256)+"."+l/(256*256)+"."+l/256+"."+l%256;
        return a+"."+b+"."+c+"."+d;
    }

    /**
     * ip转int
     * @param ip
     * @author lijie1
     * @date 2020/7/24 0024 14:16
     * @return int
     */
    public static int ip2int(String ip){
        String[] sa=ip.split("\\.");
        return Integer.parseInt(sa[0])*(256*256*256)+Integer.parseInt(sa[1])*(256*256)+Integer.parseInt(sa[2])*256+Integer.parseInt(sa[3]);
    }
}
